package com.enlorenz.common.geo.map.bing;

import com.enlorenz.common.geo.map.arcgis.ArcgisTileFilePathCreator;
import com.enlorenz.common.geo.map.base.ITileFilePathCreator;
import com.enlorenz.common.geo.map.base.TileWrapper;
import com.enlorenz.common.geo.map.google.BaseTileCmdConsole;
import com.enlorenz.util.StringUtil;


public class BingTileFileParser extends BaseTileCmdConsole {
    //URL模板
    private static String urlTemplate = "http://r3.tiles.ditu.live.com/tiles/r{0}.png?g=101&mkt=zh-cn";

    protected String getSourceUrl(TileWrapper googleTileWrapper) {
        String code = tileXYToQuadKey(Integer.valueOf(
                    googleTileWrapper.getXtile()),
                Integer.valueOf(googleTileWrapper.getYtile()),
                Integer.valueOf(googleTileWrapper.getZoom()));
        String result = StringUtil.getTempateString(urlTemplate,
                new String[] { code });

        /*        try {
                        byte [] buff=UrlUtil.readPic(result);
                        if(null == buff){
                                System.err.println(result);
                                System.err.println(getSourceUrlX(googleTileWrapper));
                        }else{
                                System.out.println(result);
                                System.out.println(getSourceUrlX(googleTileWrapper));
                        }
                } catch (Exception e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                }*/
        return result;
    }

    /*
    //URL模板
    private static String urlTemplatex="http://mt0.google.com/vt/lyrs=m@174000000&hl=zh-CN&src=app&x={0}&s=&y={1}&z={2}&s=Galileo";
    
    
    
    protected String getSourceUrlX(TileWrapper googleTileWrapper) {
            String result=PublicUtil.getTempateString(urlTemplatex,
                            new Object[]{googleTileWrapper.getXtile(),googleTileWrapper.getYtile(),googleTileWrapper.getZoom()});
            return result;
    }*/
    public static String tileXYToQuadKey(int tileX, int tileY, int levelOfDetail) {
        StringBuilder quadKey = new StringBuilder();

        for (int i = levelOfDetail; i > 0; i--) {
            char digit = '0';
            int mask = 1 << (i - 1);

            if ((tileX & mask) != 0) {
                digit++;
            }

            if ((tileY & mask) != 0) {
                digit++;
                digit++;
            }

            quadKey.append(digit);
        }

        return quadKey.toString();
    }

    protected ITileFilePathCreator createSrcCreator() {
        return new ArcgisTileFilePathCreator();
    }

    /**
     * 如果读取瓦片失败
     * @param url
     * @param googleTileWrapper
     */
    protected void readTileFailed(String url, String filePath,
        TileWrapper googleTileWrapper) {
    }


    public static void doArea(String[] args) throws Exception {
       
    }
}
